package com.cqq.print.base.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.cqq.base.filter.FMContext;
import com.cqq.common.core.base.BaseController;
import com.cqq.common.core.bean.PageInfoBean;
import com.cqq.print.base.service.PPubLevelService;
import com.cqq.print.base.service.PPublisherService;
import com.cqq.print.base.service.bean.PPubLevelBean;
import com.cqq.print.base.service.bean.PPublisherBean;
import com.cqq.pro.util.ResponseUtil;

/**
 * 2015/06/25
 *
 * @author chenqq
 */
@Controller(value = "publisherController")
@RequestMapping(value = { "publishMange" }, method = {
		RequestMethod.POST,
		RequestMethod.GET })
public class PublisherController extends BaseController{

	@Autowired
	private PPublisherService pPublisherService;
	@Autowired
	private PPubLevelService pPubLevelService;
	
	/**
	 * 查询客户列表
	 * @param request
	 * @param out
	 * @return
	 */
	@RequestMapping("list")
	public String toList(HttpServletRequest request, ModelMap out) {
		
		return "publisher/publisher";
		
	}
	
	/**
	 * 查询客户列表(异步)
	 * @param request
	 * @param out
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("listAjax")
	public String toListAjax(HttpServletRequest request, ModelMap out) throws UnsupportedEncodingException {
		String name = request.getParameter("name");		
		String currentPage = request.getParameter("currentPage");
		String pageSize = request.getParameter("pageSize");
		Map<String,Object> map = new HashMap<String, Object>();
		
		if(name!=null&&!name.trim().equals("")){
			name = URLDecoder.decode(name, "utf-8");
			map.put("name", name);
		}
		if(currentPage==null||currentPage.trim().length()<1){
			currentPage = "1";
		}
		if(pageSize==null||pageSize.trim().length()<1){
			pageSize = "20";
		}
		
		int offset = (Integer.parseInt(currentPage) - 1) * Integer.parseInt(pageSize);
		map.put("orderClause", "pp.create_date desc");
		map.put("pageSize", Integer.parseInt(pageSize));
		map.put("offset", offset);
		
		PageInfoBean<PPublisherBean> page = pPublisherService.queryPagePPublisher(map);
		page.setPageNo(Integer.parseInt(currentPage));
		page.setPageSize(Integer.parseInt(pageSize));
		
		out.put("page", page);
		return "publisher/publisher";
		
	}
	
	/**
	 * 跳转至添加客户页面
	 * @param request
	 * @param out
	 * @return
	 */
	@RequestMapping("toAddPPublisher")
	public String toAddPPublisher(HttpServletRequest request, ModelMap out) {
		//加载客户等级
		List<PPubLevelBean> listLevelBean = pPubLevelService.queryAll();
		
		out.put("userId", FMContext.getLoginUser().getId());
		out.put("realName", FMContext.getLoginUser().getRealName());
		out.put("listLevelBean", listLevelBean);
		return "publisher/addPublisher";
	}
	
	/**
	 * 新建客户
	 * @param request
	 * @param out
	 * @return
	 */
	@RequestMapping("submitPPublisher")
	public void submitPPublisher(PPublisherBean ppBean, HttpServletRequest request,HttpServletResponse response) {
		JSONObject json = new JSONObject();
		try{
			log.info("submitPPublisher ----------------------begin");
			Date currDate = new Date();
			ppBean.setCreateDate(currDate);
			ppBean.setUpdateDate(currDate);
			ppBean.setCreateId(FMContext.getCurrent().getUserId());
			int ppId = pPublisherService.insertSelective(ppBean);
			log.info("submitPPublisher ----------------------ppublisher.id:"+ppId);
			if(ppId>0){
				json.put("result", "success");
				json.put("ppId", ppId);
			}else{
				json.put("result", "faild");
				json.put("msg", "客户对象为空，请重新添加！");
			}
		}catch(Exception e){
			json.put("msg", "程序异常，请联系运维人员！");
			log.error(e.getMessage());
		}
		
		ResponseUtil.outJsonString(response, json);
	}
	
	/**
	 * 至客户详情，跳转至编辑客户页面
	 * @param request
	 * @param out
	 * @return
	 */
	@RequestMapping("toEditPPublisher")
	public String toEditPPublisher(HttpServletRequest request, ModelMap out) {
		String ppId = request.getParameter("ppId");
		PPublisherBean ppBean = new PPublisherBean();
		if(ppId!=null&&ppId.trim().length()>0){
			ppBean = pPublisherService.queryById(Integer.parseInt(ppId));
		}
		//加载客户等级
		List<PPubLevelBean> listLevelBean = pPubLevelService.queryAll();
		
		out.put("listLevelBean", listLevelBean);
		out.put("ppBean", ppBean);
		return "publisher/editPublisher";
	}
	
	/**
	 * 编辑客户
	 * @param request
	 * @param out
	 * @return
	 */
	@RequestMapping("savePPublisher")
	public void savePPublisher(PPublisherBean ppBean, HttpServletRequest request,HttpServletResponse response) {
		JSONObject json = new JSONObject();
		try{
			log.info("savePPublisher ----------------------begin");
			Date currDate = new Date();
			ppBean.setUpdateDate(currDate);
			pPublisherService.updateById(ppBean);
			log.info("savePPublisher ----------------------success");
			json.put("result", "success");
		}catch(Exception e){
			json.put("msg", "程序异常，请联系运维人员！");
			log.error(e.getMessage());
		}
		
		ResponseUtil.outJsonString(response, json);
	}
	
	/**
	 * 查询客户列表(异步)
	 * @param request
	 * @param out
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("listPPBean")
	public String toListPPBean(HttpServletRequest request, ModelMap out) throws UnsupportedEncodingException {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("orderClause", "pp.create_date desc");
		
		List<PPublisherBean> listPPBean = pPublisherService.queryPPublishers(map);
		
		out.put("listPPBean", listPPBean);
		return "publisher/publisher_alert";
		
	}
	
	/**
	 * 查询客户列表(异步)
	 * @param request
	 * @param out
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("listByName")
	public void  tolistByName(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
		String name = request.getParameter("name");		
		Map<String,Object> map = new HashMap<String, Object>();
		
		if(name!=null&&!name.trim().equals("")){
			name = URLDecoder.decode(name, "utf-8");
			map.put("name", name);
		}
		map.put("orderClause", "pp.create_date desc");
		List<PPublisherBean> listPPBean = pPublisherService.queryPPublishers(map);
		
		JSONObject json = new JSONObject();
		json.put("listPPBean", listPPBean);
		ResponseUtil.outJsonString(response, json);
	}
	
}
